
import { globalUtils } from '../../common/js/global-utils/global-utils-all';
import { uiUtils } from '../../common/js/ui-utils/ui-utils-all';
import Pure from 'pure-frontend';
import { TableSelectionTabs } from '../../common/js/table-selection-tabs';

/* SOURCE-CODE-START */

"use strict";


Pure.app.viewScope('function-management', function (scope) {
  var jqAssignResourceDialog;
  var jqTable1;
  var jqTable2;
  var jqSearchResourceBtn;
  var tableSelectionTabs;
  var dataModel;

  scope.showAssignResourceDialog = function (view, functionId) {
    if (globalUtils.isNullOrUndefined(jqAssignResourceDialog)) {
      jqAssignResourceDialog = view.$ui('assignResourceDialog');
      jqTable1 = view.$ui('assignedResourceTable');
      jqTable2 = view.$ui('assignableResourceTable');
      jqSearchResourceBtn = view.$ui('searchResourceBtn');
      dataModel = view.getDataModel();

      tableSelectionTabs = new TableSelectionTabs({
        nav1: view.$ui('assignedResourceNavTab'),
        tabPane1: view.$ui('assignedResourceTabPane'),
        table1: jqTable1,
        table1Url: 'api/security/function/listAssignedResources',
        nav2: view.$ui('assignableResourceNavTab'),
        tabPane2: view.$ui('assignableResourceTabPane'),
        table2: jqTable2,
        table2Url: 'api/security/resource/pageResourcesByCond'
      }, [
        {
          field: 'id'
        },
        {
          field: 'resourceTypeDesc'
        },
        {
          field: 'resourcePath'
        },
        {
          field: 'accessMethod'
        },
        {
          field: 'accessPolicyDesc'
        },
        {
          field: 'frozenFlagDesc'
        },
        {
          field: 'remark'
        }
      ], {
        confirmLabel: '${t("ui.security.common.operation.assign")}',
        cancelLabel: '${t("ui.security.common.operation.cancelAssign")}'
      });

      // 添加对话框事件
      addDialogEvent(view);
    }

    // 显示弹出框
    uiUtils.dialog.showModal(jqAssignResourceDialog, {
      backdropParent: view.getViewElement(),
      contextData: {
        functionId: functionId
      },
      onModalShow: function () {
        tableSelectionTabs.initOrResetTabs({
          id: functionId
        });
      }
    });
  };

  /**
   * 添加对话框事件
   * @param {*} view 
   */
  function addDialogEvent(view) {
    jqSearchResourceBtn.on(uiUtils.event.CLICK, function () {
      var data = dataModel.getData('searchResourceField.*');
      data = uiData.utils.unprefix(data, 'searchResourceField.');

      uiUtils.dataGrid.newQuery(jqTable2, {
        customQuery: data
      });
    });

    jqAssignResourceDialog.on('hide.bs.modal', function () {
      uiUtils.view.notCheckFormDataChangeWhenLeave(scope);
    });

    // table1 加载完后启用离开页面时检查是否表单发生变更
    jqTable1.on('load-success.bs.table', function () {
      uiUtils.view.setCurrentFormDataToContext(scope, getDialogData);
      uiUtils.view.checkFormDataChangeWhenLeave(scope, getDialogData);
    });

    // 点击保存按钮的事件处理
    jqAssignResourceDialog.on('click', '.x-btn-confirm', function () {
      var dialogParams = uiUtils.dialog.getContextData(jqAssignResourceDialog);
      var functionId = dialogParams.functionId;
      var resourceIds = uiUtils.dataGrid.getFieldData(jqTable1, 'id');
      var data = {
        functionId: functionId,
        resourceIds: resourceIds
      };

      view.callService('api/security/function/assignResources', data)
        .waitResult(function (assignResult) {
          if (!uiUtils.common.checkWebResultSuccess(assignResult)) {
            return;
          }

          uiUtils.dialog.hideModal(jqAssignResourceDialog);
          Pure.app.showMessage('${t("ui.system.common.msg.saveRecordSuccess")}');
        });
    });
  }

  /**
   * 返回对话框的数据
   * @returns 
   */
  function getDialogData() {
    return dataModel.getData('assignedResources');
  }

});

/* SOURCE-CODE-END */


